package cn.tedu.charging.common.uitls;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

//Json与对象转化的工具类
public class JsonUitls {

    //定义对象转换工具 ObjectMapper
    private static ObjectMapper mapper = new ObjectMapper();

    //对象转化为json
    public static String toJson(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    //Json转化为对象
    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json,clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> T convertValue(Object object,Class<T> clazz){
        return mapper.convertValue(object,clazz);
    }

}
